package com.sunseagear.common.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;

import java.util.Map;
import java.util.function.Consumer;

public class HttpUtils extends HttpUtil {

    /**
     * 异步GET请求
     *
     * @param url      请求地址
     * @param consumer 回调
     */
    public static void asyncGet(String url, Consumer<HttpResponse> consumer) {
        asyncGet(url, null, null, consumer);
    }

    /**
     * 异步GET请求
     *
     * @param url      请求地址
     * @param params   请求参数
     * @param consumer 回调
     */
    public static void asyncGet(String url, Map<String, Object> params,
                                Consumer<HttpResponse> consumer) {
        asyncGet(url, null, params, consumer);
    }

    /**
     * 异步GET请求
     *
     * @param url      请求地址
     * @param headers  请求头
     * @param params   请求参数
     * @param consumer 回调
     */
    public static void asyncGet(String url, Map<String, String> headers,
                                Map<String, Object> params,
                                Consumer<HttpResponse> consumer) {
        asyncRequest(HttpRequest.get(url), headers, params, consumer);
    }

    /**
     * 异步POST请求
     *
     * @param url      请求地址
     * @param headers  请求头
     * @param form     请求参数
     * @param consumer 回调
     */
    public static void asyncPost(String url, Map<String, String> headers,
                                 Map<String, Object> form,
                                 Consumer<HttpResponse> consumer) {
        asyncRequest(HttpRequest.post(url), headers, form, consumer);
    }

    /**
     * 异步POST请求
     *
     * @param url      请求地址
     * @param form     请求参数
     * @param consumer 回调
     */
    public static void asyncPost(String url, Map<String, Object> form,
                                 Consumer<HttpResponse> consumer) {
        asyncPost(url, null, form, consumer);
    }

    /**
     * 异步PUT请求
     *
     * @param url      请求地址
     * @param headers  请求头
     * @param form     请求参数
     * @param consumer 回调
     */
    public static void asyncPut(String url, Map<String, String> headers,
                                Map<String, Object> form,
                                Consumer<HttpResponse> consumer) {
        asyncRequest(HttpRequest.put(url), headers, form, consumer);
    }

    /**
     * 异步PUT请求
     *
     * @param url      请求地址
     * @param form     请求参数
     * @param consumer 回调
     */
    public static void asyncPut(String url, Map<String, Object> form,
                                Consumer<HttpResponse> consumer) {
        asyncPut(url, null, form, consumer);
    }

    /**
     * 异步DELETE请求
     *
     * @param url      请求地址
     * @param headers  请求头
     * @param form     请求参数
     * @param consumer 回调
     */
    public static void asyncDelete(String url, Map<String, String> headers,
                                   Map<String, Object> form,
                                   Consumer<HttpResponse> consumer) {
        asyncRequest(HttpRequest.delete(url), headers, form, consumer);
    }

    /**
     * 异步POST请求
     *
     * @param url      请求地址
     * @param form     请求参数
     * @param consumer 回调
     */
    public static void asyncDelete(String url, Map<String, Object> form,
                                   Consumer<HttpResponse> consumer) {
        asyncDelete(url, null, form, consumer);
    }

    private static void asyncRequest(HttpRequest request, Map<String, String> headers,
                                     Map<String, Object> form,
                                     Consumer<HttpResponse> consumer) {

        request.addHeaders(headers).form(form).then(consumer);
    }

}
